﻿// Author:
//   Jason Couture, plaguethenet@gmail.com
//
// Copyright (c) 2007-2011, Jason Couture
//
// All rights reserved.
//
// If you wish to use XML Settings in your application, YOU MUST FIRST request permission.
// This source is provided to the llAma project to ease the build process on linux systems.
// It may be modified and used with llAma only, And not in another project, or released on
// its own, Without my express written permssion. AKA, ALL RIGHTS RESERVED.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Collections.Generic;
using System.Text;

namespace XMLSettings
{
    public static class SPath
    {
        
        private static Setting Process(Setting Root, string Query, bool InternalCall)
        {
            //tokenize the query
            if (string.Compare(Query, "@list", true) == 0)
            {
                List<string> ChildNames = new List<string>();
                Setting[] children = Root.GetChildList().ToArray();
                foreach (Setting s in children)
                {
                    ChildNames.Add(s.Name);
                }
                return new Setting("keylist", string.Join(", ", ChildNames.ToArray()), null);
            }
            if (string.Compare(Query, "@clear", true) == 0)
            {
                Root.Clear();
                return Root;
            }
            if (string.Compare(Query, "@delete", true) == 0)
            {
                string keyName = Root.Name;
                if (Root.Parent != null)
                {
                    Root = Root.Parent;
                    Root[keyName] = null;
                    return Setting.NullSetting;
                }
                else
                {
                    return new Setting("error", "Cannot delete a root key.");
                }
            }
            if (Query.StartsWith("/"))
                return Process(Root.Root, Query.Substring(1), true);
            if (Query.CompareTo(string.Empty) == 0)
                return Root;
            if (Query.IndexOf("=") != -1)
            {
                if (Query.IndexOf("/") != -1)
                {
                    if (Query.IndexOf("/") < Query.IndexOf("="))
                    {
                        if (Query.Length > (Query.IndexOf("/") + 1))
                        {
                            return Process(Root[Query.Substring(0, Query.IndexOf("/"))], Query.Substring(Query.IndexOf("/") + 1), true);
                        }
                        else
                        {
                            return Setting.NullSetting;
                        }
                    }
                    else
                    {
                        if (Query.Length > (Query.IndexOf("=") + 1))
                        {
                            Root[Query.Substring(0, Query.IndexOf("="))] = Query.Substring(Query.IndexOf("=") + 1).Trim();
                            return Root[Query.Substring(0, Query.IndexOf("="))];
                        }
                        else
                        {
                            Root[Query.Substring(0, Query.IndexOf("="))] = string.Empty;
                            return Root[Query.Substring(0, Query.IndexOf("="))];
                        }
                    }
                }
                else
                {
                    if (Query.Length > (Query.IndexOf("=") + 1))
                    {
                        Root[Query.Substring(0, Query.IndexOf("="))] = Query.Substring(Query.IndexOf("=") + 1).Trim();
                        return Root[Query.Substring(0, Query.IndexOf("="))];
                    }
                    else
                    {
                        Root[Query.Substring(0, Query.IndexOf("="))] = string.Empty;
                        return Setting.NullSetting;
                    }
                }
            }
            else
            {
                //Get only.
                if (Query.IndexOf("/") != -1)
                {
                    return Process(Root[Query.Substring(0, Query.IndexOf("/"))], Query.Substring(Query.IndexOf("/") + 1), true);
                }
                else
                {
                    if (Query.Trim() != "")
                        return Root[Query];
                    else
                        return Root;
                }
            }
        }
        public static Setting Process(Setting Root, string Query)
        {
            return Process(Root, Query, false);
        }

    }
}
